<template>
	<view>
	<!-- 	<view class="search-box">
			<uni-search-bar cancelButton="none" v-model="searchKeyWord" :radius="100"></uni-search-bar>	
		</view> -->
		<u-navbar>
			<u-search :action-style="{
				color:'#fff',backgroundImage: 'linear-gradient(to right, #ff9000 0, #ff5000 100%)',borderRadius:'11px',width:'120rpx',height:'59rpx',margin:'0 10px',lineHeight:'59rpx'
			}" action-text="搜索" :animation="true" search-icon="" placeholder="请输入搜索内容" v-model="searchKeyWord"></u-search>
		</u-navbar>
		
		<view class="sugg-list">
			<view v-for="(item,index) in searchResults" :key="index" class="sugg-item" @click="gotoDetail(item)">
				<icon size="15" class="search-icon" type="search"></icon>
				<view class="goods-name">{{item.goods_name}}</view>
			</view>
		</view>
		
		<view class="history-box">
			
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons @click="clearHistory" type="trash" size="17"></uni-icons>
			</view>
			
			<view class="history-list">
				<uni-tag @click="gotoGoodsList(item)" :text="item" v-for="(item,index) in historys" :key="index"></uni-tag>
			</view>
			
		</view>
		
		
	</view>
</template>
	
<script>
	export default {
		data() {
			return {
				searchKeyWord:'',
				timer:null,
				searchResults:[],
				historyList:['耳机','apple','短袖'],
			};
		},
		watch:{
			searchKeyWord(value){
				clearTimeout(this.timer)
				this.timer = setTimeout(async ()=>{
					this.searchKeyWord = value.trim()
					if(this.searchKeyWord !== ''){
						let {data:{message,meta}} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.searchKeyWord})
						
						const set = new Set(this.historyList)
						set.delete(this.searchKeyWord)
						set.add(this.searchKeyWord)
						this.historyList = Array.from(set)
						uni.setStorageSync('kw',JSON.stringify(this.historyList))
						
						if(meta.status !== 200) return uni.$showMsg(meta.msg)
						this.searchResults = message
						
					}else this.searchResults = []
				},400)
			}
		},
		computed: {
		  historys() {
		    // 注意：由于数组是引用类型，所以不要直接基于原数组调用 reverse 方法，以免修改原数组中元素的顺序
		    // 而是应该新建一个内存无关的数组，再进行 reverse 反转
		    return [...this.historyList].reverse()
		  }
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods:{
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			clearHistory(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			gotoGoodsList(key){
				console.log(key)
				uni.navigateTo({
					url:"/subpkg/goods_list/goods_list?cil=" + key
				})
			}
		}
	}
</script>

<style lang="scss">
	.history-box{
		padding: 0 10px;
		
		.history-title{
			padding: 10px 0;
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #efefef;
			margin-bottom: 10px;
		}
		
		.history-list{
			display: flex;
			flex-wrap: wrap;
			
			
			.uni-tag{
				margin-right: 7px;
				margin-bottom: 5px;
			}
		}
	}
	
	.sugg-list{
		
		.sugg-item{
			display: flex;
			height: 40px;
			align-items: center;
			border-bottom: 1px solid #efefef;
			
			
			.goods-name{
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				font-size: 14px;
			}
			.search-icon{
				margin-right: 7px;
				padding-left: 10px;
			}
		}
	}
	
	// .search-box{
	// 	background-color: #ff0000;
	// }

.uni-searchbar {
  background-color: #fff;
}


</style>
